const express = require('express');
var logger = require('morgan');
const app = express();

// 打印请求日志
app.use(logger('dev'));

app.use(express.json()) // 增加 application/json 请求类型的支持
app.use(express.urlencoded({ extended: true })) // 增加 application/x-www-form-urlencoded 请求类型的支持

// queryString参数
app.get('/getUrl', function (req, res) {
  console.log(req.query);
  res.end(JSON.stringify(req.query));
})
// restful参数
app.get('/getUrl/:name', function (req, res) {
  console.log(req.params.name);
  res.send(req.params.name);
})
// 请求体参数，上述两行app.use就是为了支持请求体参数
app.post('/postUrl', function (req, res) {
  console.log(req.body);
  res.json({"postMsg": req.body});
})
app.put('/putUrl', function (req, res) {
  console.log(req.body);
  res.json({"putMsg": req.body});
})
app.delete('/deleteUrl', function (req, res) {
  console.log(req.body);
  res.json({"deleteMsg": req.body});
})


//监听端口
var server = app.listen(8088, 'localhost', function () {
  var host = server.address().address
  var port = server.address().port
  console.log("webserver已启动，地址：http://%s:%s", host, port)
});